package com.gulimall.search.controller;

import com.gulimall.common.exception.BizCodeEnum;
import com.gulimall.common.to.SkuEsModel;
import com.gulimall.common.utils.R;
import com.gulimall.search.service.ElasticSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author kkk
 * @date 2022/7/21
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {
    @Autowired
    private ElasticSaveService elasticSaveService;

    @PostMapping("/product")
    private R productUp(@RequestBody List<SkuEsModel> esModelList) {
        boolean res = false;

        try {
            res = elasticSaveService.saveProduct(esModelList);
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架错误 {} ", e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if (!res) {
            return R.ok();
        }
        return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
